//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;

namespace Tathata.Common.DAL.Managers
{
    internal class LogDBManager
    {
        #region Methods
        public static void DeleteLog(int LogID)
        {
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_LogDelete");
            db.AddInParameter(dbCommand, "LogID", DbType.Int32, LogID);
            int retValue = db.ExecuteNonQuery(dbCommand);
        }

        public static void ClearLog()
        {
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_LogClear");
            int retValue = db.ExecuteNonQuery(dbCommand);
        }

        public static LogCollection GetAllLogs()
        {
            LogCollection logCollection = new LogCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_LogLoadAll");
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    Log log = new Log();
                    log.LogID = NopDataHelper.GetInt(dataReader, "LogID");
                    log.LogTypeID = NopDataHelper.GetInt(dataReader, "LogTypeID");
                    log.Severity = NopDataHelper.GetInt(dataReader, "Severity");
                    log.Message = NopDataHelper.GetString(dataReader, "Message");
                    log.Exception = NopDataHelper.GetString(dataReader, "Exception");
                    log.IPAddress = NopDataHelper.GetString(dataReader, "IPAddress");
                    log.CustomerID = NopDataHelper.GetInt(dataReader, "CustomerID");
                    log.PageURL = NopDataHelper.GetString(dataReader, "PageURL");
                    log.CreatedOn = NopDataHelper.GetDateTime(dataReader, "CreatedOn");
                    logCollection.Add(log);
                }
            }

            return logCollection;
        }

        public static Log GetByLogID(int LogID)
        {
            Log log = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_LogLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "LogID", DbType.Int32, LogID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    log = new Log();
                    log.LogID = NopDataHelper.GetInt(dataReader, "LogID");
                    log.LogTypeID = NopDataHelper.GetInt(dataReader, "LogTypeID");
                    log.Severity = NopDataHelper.GetInt(dataReader, "Severity");
                    log.Message = NopDataHelper.GetString(dataReader, "Message");
                    log.Exception = NopDataHelper.GetString(dataReader, "Exception");
                    log.IPAddress = NopDataHelper.GetString(dataReader, "IPAddress");
                    log.CustomerID = NopDataHelper.GetInt(dataReader, "CustomerID");
                    log.PageURL = NopDataHelper.GetString(dataReader, "PageURL");
                    log.CreatedOn = NopDataHelper.GetDateTime(dataReader, "CreatedOn");
                }
            }
            return log;
        }

        public static Log InsertLog(int LogTypeID, int Severity, string Message,
            string Exception, string IPAddress, int CustomerID, string PageURL, DateTime CreatedOn)
        {
            Log log = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_LogInsert");
            db.AddOutParameter(dbCommand, "LogID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "LogTypeID", DbType.Int32, LogTypeID);
            db.AddInParameter(dbCommand, "Severity", DbType.Int32, Severity);
            db.AddInParameter(dbCommand, "Message", DbType.String, Message);
            db.AddInParameter(dbCommand, "Exception", DbType.String, Exception);
            db.AddInParameter(dbCommand, "IPAddress", DbType.String, IPAddress);
            db.AddInParameter(dbCommand, "CustomerID", DbType.Int32, CustomerID);
            db.AddInParameter(dbCommand, "PageURL", DbType.String, PageURL);
            db.AddInParameter(dbCommand, "CreatedOn", DbType.DateTime, CreatedOn);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                int LogID = Convert.ToInt32(db.GetParameterValue(dbCommand, "@LogID"));
                log = GetByLogID(LogID);
            }
            return log;
        }
        #endregion
    }
}
